package com.my.oschina;

import android.os.Bundle;

import com.actionbarsherlock.view.Window;
import com.my.oschina.app.AppContext;
import com.my.oschina.fragment.InfoFragment;
import com.my.oschina.fragment.MenuFragment;
import com.my.oschina.fragment.QuestionFragment;
import com.my.oschina.fragment.TweetFragment;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// 注意：必须在添加内容前设置
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);

		// 初始化登录用户的信息，包括读取保存的信息
		((AppContext) getApplication()).initLoginInfo();

		// set the Above View
		setContentView(R.layout.content_frame);
		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new MenuFragment()).commit();
		// customize the SlidingMenu
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

	}

	/**
	 * 返回侧边导航MenuFragment
	 * 
	 * @return fragment
	 */
	public MenuFragment getMenuFragment() {
		MenuFragment fragment = ((MenuFragment) getSupportFragmentManager()
				.findFragmentByTag(MenuFragment.class.getName()));
		if (fragment == null) {
			fragment = new MenuFragment();
		}
		return fragment;
	}

	/**
	 * 返回InfoFragment
	 * 
	 * @return fragment
	 */
	public InfoFragment getInfoFragment() {
		InfoFragment fragment = ((InfoFragment) getSupportFragmentManager()
				.findFragmentByTag(InfoFragment.class.getName()));
		if (fragment == null) {
			fragment = new InfoFragment();
		}

		return fragment;
	}

	/**
	 * 返回QuestionFragment
	 * 
	 * @return fragment
	 */
	public QuestionFragment getQuestionFragment() {
		QuestionFragment fragment = ((QuestionFragment) getSupportFragmentManager()
				.findFragmentByTag(QuestionFragment.class.getName()));
		if (fragment == null) {
			fragment = new QuestionFragment();
		}

		return fragment;
	}

	/**
	 * 返回TweetFragment
	 * 
	 * @return fragment
	 */
	public TweetFragment getTweetFragment() {
		TweetFragment fragment = ((TweetFragment) getSupportFragmentManager()
				.findFragmentByTag(TweetFragment.class.getName()));
		if (fragment == null) {
			int uid = ((AppContext) getApplication()).getLoginUid();
			fragment = TweetFragment.newInstance(uid);
		}

		return fragment;
	}

}
